package dao;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CacheRetrieveMode;
import javax.persistence.EntityManager;
import javax.persistence.Query;

import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;

import model.*;

import util.DateAndString;
import util.EntityManagerUtil;

public class ViewDao {
	public static List<View> getViews() {
		EntityManager em = EntityManagerUtil.getEntityManager();
		Query query = em.createQuery("SELECT c FROM View c ");
		query.setHint("javax.persistence.cache.retrieveMode", CacheRetrieveMode.BYPASS);
		List<View> views = query.getResultList();
		em.close();
		return views;
	}

	public static View getView(View view) {
		EntityManager em = EntityManagerUtil.getEntityManager();
		View viewFind = new View();
		viewFind = em.find(View.class, view.getName());
		if (viewFind != null) {
			em.close();
			return viewFind;
		} else
			return null;
	}

	public static View getView(String vname) {
		EntityManager em = EntityManagerUtil.getEntityManager();
		View viewFind = new View();
		viewFind = em.find(View.class, vname);
		if (viewFind != null) {
			em.close();
			return viewFind;
		} else
			return null;
	}

	public static View getViewNoCache(String vname) throws Exception {
		EntityManager em = EntityManagerUtil.getEntityManager();
		String sql = "select c from View c where c.name=:name";
		Query query = em.createQuery(sql);
		query.setHint("javax.persistence.cache.retrieveMode", CacheRetrieveMode.BYPASS);
		View viewFind;
		try {
			viewFind = (View) query.setParameter("name", vname).getSingleResult();
			em.close();
			return viewFind;
		} catch (Exception e) {
			em.close();
			e.printStackTrace();
			return null;
			// TODO Auto-generated catch block

		}

	}

	public static boolean getViewNoCacheForBool(String vname) throws Exception {
		EntityManager em = EntityManagerUtil.getEntityManager();
		String sql = "select c from View c where c.name=:name";
		Query query = em.createQuery(sql);
		query.setHint("javax.persistence.cache.retrieveMode", CacheRetrieveMode.BYPASS);
		View viewFind;
		try {
			viewFind = (View) query.setParameter("name", vname).getSingleResult();
			em.close();
			return true;
		} catch (Exception e) {
			em.close();
			return false;
		}

	}

	public static View getViewNoCache(View view) throws Exception {
		EntityManager em = EntityManagerUtil.getEntityManager();
		String sql = "select c from View c where c.name=:name";
		Query query = em.createQuery(sql);
		query.setHint("javax.persistence.cache.retrieveMode", CacheRetrieveMode.BYPASS);
		View viewFind;
		try {
			viewFind = (View) query.setParameter("name", view.getName()).getSingleResult();
			em.close();
			return viewFind;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			em.close();
			return null;
		}
	}

	public static boolean addView(View view) throws Exception {
		EntityManager em = EntityManagerUtil.getEntityManager();
		if (!getViewNoCacheForBool(view.getName())) {
			em.getTransaction().begin();
			em.persist(view);
			em.getTransaction().commit();
			em.close();
			return true;
		} else {
			return false;
		}

	}

	public static boolean delView(View view) throws Exception {
		EntityManager em = EntityManagerUtil.getEntityManager();
		try {
			View viewFind = getViewNoCache(view);
			if (viewFind != null) {
				View delView = em.find(View.class, view.getName());
				em.getTransaction().begin();
				List<Vroute> vroutes = new ArrayList<Vroute>();
				vroutes = delView.getVroutes();
				if (vroutes != null) {
					for (Vroute vroute : vroutes) {
						VrouteDao.delVroute(vroute);
						// RouteDao.delRoute(route);
					}

				}
				List<Message> messages = new ArrayList<Message>();
				messages = delView.getMessages();
				for (Message message : messages) {
					MessageDao.delMessage(message);
				}

				em.remove(delView);
				em.getTransaction().commit();
				em.close();
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			return false;
		}

	}

	public static boolean updateView(View view) throws Exception {
		EntityManager em = EntityManagerUtil.getEntityManager();
		try {
			View viewFind = getViewNoCache(view);
			if (viewFind != null) {
				View upView = em.find(View.class, view.getName());
				em.getTransaction().begin();
				upView.setInfo(view.getInfo());
				upView.setLocation(view.getLocation());
				upView.setTime(view.getTime());
				upView.setPrice(view.getPrice());
				// dem.persist(vorders);
				em.getTransaction().commit();
				em.close();

				List<Vroute> vroutes = new ArrayList<Vroute>();
				vroutes = upView.getVroutes();
				for (Vroute vroute : vroutes) {
					RouteDao.refreshMoney(vroute.getRoute());
				}
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			return false;
		}

	}

}
